package cn.codeartist.wechat.qywx.service.auth.impl;

import cn.codeartist.springboot.component.http.proxy.HttpProxy;
import cn.codeartist.wechat.qywx.entity.QywxGlobalInfo;
import cn.codeartist.wechat.qywx.entity.QywxGlobalInfo.QywxApplicationInfo;
import cn.codeartist.wechat.qywx.entity.constants.QywxApiUrl;
import cn.codeartist.wechat.qywx.entity.dto.auth.QywxAccessTokenDTO;
import cn.codeartist.wechat.qywx.service.auth.QywxAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * 企业微信权限相关接口
 *
 * @author 艾江南
 * @date 2020/7/28
 */
@Service
public class QywxAuthServiceImpl implements QywxAuthService {

    @Autowired
    private QywxGlobalInfo qywxGlobalInfo;
    @Autowired
    private QywxApplicationInfo qywxApplicationInfo;
    @Autowired
    private HttpProxy qywxHttpProxy;

    @Override
    public QywxAccessTokenDTO getAccessToken(String corpsecret) {
        return qywxHttpProxy.get(QywxApiUrl.GET_ACCESS_TOKEN, QywxAccessTokenDTO.class, qywxGlobalInfo.getCorpid(), corpsecret);
    }

    @Override
    @Cacheable("QYWX_CONTACT_ACCESS_TOKEN")
    public String getContactAccessToken() {
        QywxAccessTokenDTO accessToken = getAccessToken(qywxApplicationInfo.getContactSecret());
        return accessToken.getAccessToken();
    }
}
